
#include "StdAfx.h"
#include "LinkComboBox.h"
#include "LinkInputNode.h"

CLinkComboBox::CLinkComboBox(INPUT_FIELD_INFO* pFieldInfo) : CComboBox(), CLinkCtrl(pFieldInfo)
{
	CLinkInputNode::AddInput(this, CtrlType);
	
	if(pFieldInfo)
	{
		pFieldInfo->pLinkCtrl = this;
		pFieldInfo->dwCtrlType = CtrlType;
	}
}

CLinkComboBox::~CLinkComboBox()
{
	CLinkInputNode::DelInput(this);
}

CString	CLinkComboBox::GetControlData(BOOL bTrimBlank, BOOL bReplaceSingleQuotation)
{
	int nSel = GetCurSel();
	CString strValue;
	strValue.Format(_T("%d"), nSel);

	return strValue;
}

BOOL CLinkComboBox::PreTranslateMessage(MSG* pMsg)
{
	return CLinkCtrl::PreTranslateMessage(pMsg, this);
}

void CLinkComboBox::PreSubclassWindow()
{
	CLinkCtrl::PreSubclassWindow(this);
	CComboBox::PreSubclassWindow();
}

BOOL CLinkComboBox::CopyFrom(CLinkComboBox* pFrom)
{
	if(!pFrom)
		return FALSE;
	
	CString strValue = pFrom->GetControlData();
	SetCurSel(_ttoi(strValue));
	
	return TRUE;
}

void CLinkComboBox::ScrollToVisible()
{
	CWnd* pParent = GetParent();
	if(pParent)
	{
		LONG lStyle = GetWindowLong(pParent->GetSafeHwnd(), GWL_STYLE);
		if(lStyle & WS_VSCROLL)
		{
			RECT rectChild, rectParent;
			GetWindowRect(&rectChild);
			pParent->GetWindowRect(&rectParent);
			
			while(rectChild.top < rectParent.top)
			{
				::SendMessage(pParent->GetSafeHwnd(), WM_VSCROLL, SB_PAGEUP, NULL);
				GetWindowRect(&rectChild);
				pParent->GetWindowRect(&rectParent);
			}
			
			while(rectChild.bottom > rectParent.bottom)
			{
				::SendMessage(pParent->GetSafeHwnd(), WM_VSCROLL, SB_PAGEDOWN, NULL);
				GetWindowRect(&rectChild);
				pParent->GetWindowRect(&rectParent);
			}
		}
	}
}
